package com.ouc.openplatform.ouc.authManage.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ouc.openplatform.ouc.authManage.dto.NJ_PageAuthorityDTO;
import com.ouc.openplatform.ouc.authManage.dto.NJ_RoleAuthorityDTO;
import com.ouc.openplatform.ouc.authManage.model.NJ_RoleAuthority;
import com.ouc.openplatform.ouc.authManage.service.NJ_ClientRoleAuthorityService;
import com.ouc.openplatform.ouc.authManage.service.NJ_RoleAuthorityService;
import com.ouc.openplatform.ouc.authManage.vo.NJ_RoleAuthorityVo;
import com.ouc.openplatform.pojo.EUDataGridResult;

public class NJ_ClientRoleAuthorityServiceImpl implements NJ_ClientRoleAuthorityService {

	private NJ_RoleAuthorityService nj_roleAuthorityService;

	public NJ_RoleAuthorityService getNj_roleAuthorityService() {
		return nj_roleAuthorityService;
	}

	public void setNj_roleAuthorityService(NJ_RoleAuthorityService nj_roleAuthorityService) {
		this.nj_roleAuthorityService = nj_roleAuthorityService;
	}
	//1.增加角色权限信息-----sxl
	@Override
	public int saveRoleAuthority(NJ_RoleAuthorityDTO roleAuthorityDTO) {
		NJ_RoleAuthority roleAuthority = new NJ_RoleAuthority();
		roleAuthority.setAdduser(roleAuthorityDTO.getAdduser());

		SimpleDateFormat myFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date date = myFmt.parse(roleAuthorityDTO.getAddtime().toString());
			roleAuthority.setAddtime(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		roleAuthority.setAuthoritydetail(roleAuthorityDTO.getAuthoritydetail());
		roleAuthority.setAuthorityid(roleAuthorityDTO.getAuthorityid());
		roleAuthority.setRoleid(roleAuthorityDTO.getRoleid());
		roleAuthority.setVisible(roleAuthorityDTO.getVisible());
		int flag = nj_roleAuthorityService.saveRoleAuthority(roleAuthority);
		return flag;
	}
	//2.删除角色权限信息-----sxl
	@Override
	public int deleteRoleAuthority(int id) {
		int flag = nj_roleAuthorityService.deleteRoleAuthority(id);
		return flag;
	}
	//3.修改角色权限信息-----sxl
	@Override
	public int updateRoleAuthority(NJ_RoleAuthorityDTO roleAuthorityDTO) {
		NJ_RoleAuthority roleAuthority = new NJ_RoleAuthority();
		roleAuthority.setAdduser(roleAuthorityDTO.getAdduser());
		SimpleDateFormat myFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date date = myFmt.parse(roleAuthorityDTO.getAddtime().toString());
			roleAuthority.setAddtime(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		// roleAuthority.setAddtime(roleAuthorityDTO.getAddtime());
		roleAuthority.setAuthoritydetail(roleAuthorityDTO.getAuthoritydetail());
		roleAuthority.setAuthorityid(roleAuthorityDTO.getAuthorityid());
		roleAuthority.setRoleid(roleAuthorityDTO.getRoleid());
		roleAuthority.setVisible(roleAuthorityDTO.getVisible());
		int flag = nj_roleAuthorityService.updateRoleAuthority(roleAuthority);
		return flag;
	}

	//4.查询所有角色权限信息----sxl
	@Override
	public EUDataGridResult getAllRoleAuthority(int page, int rows) {
		return nj_roleAuthorityService.getAllRoleAuthority(null,page,rows);
	}
	@Override
	public List<NJ_RoleAuthority> getRoleAuthority(NJ_RoleAuthority roleauthority) {
		return nj_roleAuthorityService.getRoleAuthority(roleauthority);
	}

	// 根据角色id获取所有的权限-----SXL
	@Override
	public List<NJ_PageAuthorityDTO> getAllJoinRoleAuthorityByroleid(int roleid) {
		List<NJ_PageAuthorityDTO> nj_PageAuthorityDTOList = new ArrayList<NJ_PageAuthorityDTO>();
		List<NJ_RoleAuthorityVo> nj_joinRoleAuthorityList = nj_roleAuthorityService.getAllJoinRoleAuthorityByroleid(roleid);
		for (int i = 0; i < nj_joinRoleAuthorityList.size(); i++) {
			NJ_PageAuthorityDTO nj_PageAuthorityDTO = new NJ_PageAuthorityDTO();
			nj_PageAuthorityDTO.setId(i);
			nj_PageAuthorityDTO.setPageurl(nj_joinRoleAuthorityList.get(i).getNj_AuthorityInfo().getPageurl());
			nj_PageAuthorityDTO.setAuthority(nj_joinRoleAuthorityList.get(i).getNj_AuthorityInfo().getAuthority());
			nj_PageAuthorityDTOList.add(nj_PageAuthorityDTO);
		}
		return nj_PageAuthorityDTOList;
	}
}
